package com.shijie.sjoj.router.filter;

public class example {
    /*
        public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        Mono<Void> mono = chain.filter(exchange);
        if (exchange.getRequest().getPath().toString().equals("/login")) {
            ServerHttpRequest request = exchange.getRequest();
            // 必须将该流式返回才能执行
            mono = DataBufferUtils.join(request.getBody())
                    .flatMap(dataBuffer -> {
                        log.info("\033[33m====== 开始请求体过滤 ======\033[0m");
                        // 获取字节数大小
                        int count = dataBuffer.readableByteCount();
                        log.info("\033[33m字节数：{}\033[0m", count);
                        // 设置字节数组
                        byte[] bytes = new byte[count];
                        // 读取数据
                        dataBuffer.read(bytes);
                        // 释放缓冲区
                        DataBufferUtils.release(dataBuffer);
                        // 转为字符串
                        String body = new String(bytes, StandardCharsets.UTF_8);
                        log.info("\033[33m请求体：{}\033[0m", body);
                        // 从原请求重新构建请求
                        ServerHttpRequestDecorator newRequest = new ServerHttpRequestDecorator(request) {
                            // 设置请求体
                            @Override
                            public Flux<DataBuffer> getBody() {
                                return Flux.just(new DefaultDataBufferFactory().wrap(bytes));
                            }
                        };
                        // 构建返回值调用链
                        Mono<Void> mono1 = chain.filter(exchange.mutate().request(newRequest).build());
                        log.info("\033[33m====== 结束请求体过滤 ======\033[0m");
                        return mono1;
                    });
        }
        return mono;
    }
    */
}
